import java.awt.Image;
import java.util.ArrayList;

public class Animation {
	
	private ArrayList<OneScene> scenes;
	private int sceneIndex;
	private long movieTime;
	private long totalTime;
	public boolean finished;
	//constructor
	public Animation(){
		scenes = new ArrayList<OneScene>();
		totalTime = 0;
		finished = false;
		start();
	}
	
	//add scene to ArrayList and set time for each scene
	public synchronized void addScene(Image img, long time){
		totalTime += time;
		scenes.add(new OneScene(img, totalTime));
	}
	
	//start animation from beginning
	public synchronized void start(){
		movieTime = 0;
		sceneIndex = 0;
		finished = false;
	}
	
	//change scenes
	public synchronized void update(long timePassed){
		if(scenes.size() > 1){
			movieTime += timePassed;
			if(movieTime >= totalTime){
				finished = true;
			}
			while(movieTime > getScene(sceneIndex).endTime && !finished){
				sceneIndex++;
			}
		}
	}
	
	//get animation current scene
	public synchronized Image getImage(){
		if(scenes.size() == 0){
			return null;
		}else{
			return getScene(sceneIndex).pic;
		}
	}
	
	public int getSceneNumber(){
		return sceneIndex+1;
	}
	
	//get scene
	private OneScene getScene(int x){
		return (OneScene)scenes.get(x);
	}
	
	
////////PRIVATE INNER CLASS /////////////////
	private class OneScene{
		Image pic;
		long endTime;
		
		public OneScene(Image pic, long endTime){
			this.pic = pic;
			this.endTime = endTime;
		}
	}
	
}
